package main

import (
	"gameServer/cluster/gate"
	"gameServer/log"
	"gameServer/registry/memory"
	"time"
)

func main() {
	// 创建内存注册中心
	registry := memory.NewMemoryRegistry()

	// 创建网关组件并配置注册中心
	gateComponent := gate.NewGate().SetTcpServer(":52051").SetRegistry(registry)

	// 模拟启动网关
	if err := gateComponent.Init(); err != nil {
		log.Fatalf("Init failed: %v", err)
	}

	if err := gateComponent.Start(); err != nil {
		log.Fatalf("Start failed: %v", err)
	}

	// 等待一段时间让服务注册完成
	time.Sleep(1 * time.Second)

	// 查询注册的服务
	services, err := registry.GetService("gate")
	if err != nil {
		log.Fatalf("GetService failed: %v", err)
	}

	log.Infof("Found %d gate services:", len(services))
	for _, service := range services {
		log.Infof("  - ID: %s, Address: %s:%d", service.ID, service.Address, service.Port)
	}

	// 模拟停止网关
	if err := gateComponent.Stop(); err != nil {
		log.Fatalf("Stop failed: %v", err)
	}

	// 再次查询注册的服务
	services, err = registry.GetService("gate")
	if err != nil {
		log.Fatalf("GetService failed: %v", err)
	}

	log.Infof("After stop, found %d gate services", len(services))
}
